<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入访客姓名" suffix-icon="el-icon-search" v-model="visName"></el-input>
      <el-select v-model="visHelthCode" placeholder="请选择建康码状态">
        <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>

      <el-select v-model="visType" placeholder="请选择进出类型">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="您确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>

    <el-table :data="tableData.slice((pageNum-1)*pageSize,pageNum*pageSize)" style="width: 100%" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="visitorId" label="ID" width="50"></el-table-column>
      <el-table-column prop="visName" label="姓名" width="80"></el-table-column>
      <el-table-column prop="visHelthCode" label="建康码状态" width="100">
        <template slot-scope="scope">
          <p v-if="scope.row.visHelthCode === 0">绿码</p>
          <p v-if="scope.row.visHelthCode === 1">黄码</p>
          <p v-if="scope.row.visHelthCode === 2">红码</p>
        </template>
      </el-table-column>
      <el-table-column prop="visType" label="出入类型" width="80">
        <template slot-scope="scope">
          <p v-if="scope.row.visType === 0">进</p>
          <p v-else>出</p>
        </template>
      </el-table-column>
      <el-table-column prop="visPhone" label="电话" width="120"></el-table-column>
      <el-table-column prop="visAddress" label="社区"></el-table-column>
      <el-table-column prop="house" label="楼号"></el-table-column>
      <el-table-column prop="unit" label="单元号"></el-table-column>
      <el-table-column prop="visRemark" label="备注"></el-table-column>
      <el-table-column prop="visTime" label="受访时间"  width="100"></el-table-column>
      <el-table-column prop="resRemarks" label="备注"></el-table-column>
      <el-table-column prop="visStatus" label="状态">
        <template slot-scope="scope">
          <p v-if="scope.row.visStatus === 0">正常</p>
        </template>
      </el-table-column>
      <el-table-column label="操作"  width="200" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.visitorId)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

  </div>
</template>
<script>

export default {
  name: "VisitorInformation",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      visName: "",
      house: "",
      visType: "",
      visitorId: "",
      resRemarks: "",
      visHelthCode: "",
      resStatus: "",
      unit: "",
      visStatus:"",
      resState: "",
      dialogFormVisible: false,
      dialogForm: false,
      multipleSelection: [],
      options: [{
        value: '0',
        label: '进'
      }, {
        value: '1',
        label: '出'
      }],
      options1: [{
        value: '0',
        label: '绿码'
      }, {
        value: '1',
        label: '黄码'
      }, {
        value: '2',
        label: '红码'
      }],
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/vis/getAll", {
        params: {
          visName: this.visName,
          visHelthCode: this.visHelthCode,
          visType: this.visType,
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.total

      })
    },
    del(id) {
      this.request.post("/vis/upDelete/" + id).then(res => {
        if (res.status == 201) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.visitorId)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/vis/upDelete/batch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    reset() {
      this.visName = ""
      this.visHelthCode = ""
      this.visType = ""
      this.load()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum = 1;
      this.pageSize = val;
      this.load()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.load()
    }
  }
}
</script>


<style>
.headerBg {
  background: #eee!important;
}
</style>